package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.ReserveInformation;
import com.ruoyi.personnel.service.IReserveInformationService;
import com.ruoyi.personnel.vo.OpenIdVO;
import com.ruoyi.personnel.vo.ReserveInformationVO;
import com.ruoyi.personnel.vo.StatusAndOpenIdVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 *
 * @author hyj
 * @date 2020-02-28
 */
@Controller
@RequestMapping("/wx/access")
public class ReserveInformationController  extends BaseController {

    private String prefix = "wx/access";

    @Autowired
    private IReserveInformationService reserveInformationService;

    @RequiresPermissions("wx:access:view")
    @GetMapping()
    public String access()
    {
        return prefix + "/access";
    }

    /**
     * 提交预约信息
     * @param reserveInformationVO 预约信息VO对象
     * @return 是否成功
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult saveReserveInformationService(@Validated  @RequestBody ReserveInformationVO reserveInformationVO){
        return toAjax(reserveInformationService.saveReserveInformationService(reserveInformationVO));
    }


    /**
     * 查看预约信息
     * @param statusAndOpenIdVO 用户id 例: 071mUNAd0ZBHbz1CZrCd0rZMAd0mUNA0
     * @return 表格对象
     */
    @PostMapping("/find")
    @ResponseBody
    public TableDataInfo list(@Validated @RequestBody StatusAndOpenIdVO statusAndOpenIdVO)
    {
        startPage();
        List<ReserveInformationVO> list = reserveInformationService.selectReserveInformationList(statusAndOpenIdVO);
        return getDataTable(list);
    }

    /**
     * 修改预约信息
     * @param reserveInformationVO
     * @return 是否成功修改
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult edit(@RequestBody ReserveInformationVO reserveInformationVO){
        return toAjax(reserveInformationService.updateReserveInformation(reserveInformationVO));
    }

    /**
     * 檢查我的預約列表
     * @param reserveInformationVO
     * @return
     */
    @PostMapping("/checkout")
    @ResponseBody
    public AjaxResult checkOut(@RequestBody ReserveInformationVO reserveInformationVO){
        Long id = reserveInformationVO.getId();
        return reserveInformationService.selectReserveInformationByIdToAjaxResult(id);
    }

    /**
     * 修改预约状态
     * @param statusAndOpenIdVO
     * @return
     */
    @PostMapping("/sureVisit")
    @ResponseBody
    public AjaxResult editStatus(@RequestBody StatusAndOpenIdVO statusAndOpenIdVO){
        return toAjax(reserveInformationService.updateReserveInformation(statusAndOpenIdVO));
    }

    /**
     * 修改预约信息
     * @param reserveInformationVO
     * @return 是否成功修改
     */
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestBody ReserveInformationVO reserveInformationVO){
        Long id = reserveInformationVO.getId();
        return toAjax(reserveInformationService.deleteReserveInformationById(id));
    }


    /**
     * 查询预约信息列表
     */
    @RequiresPermissions("wx:access:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReserveInformation reserveInformation)
    {
        startPage();
        List<ReserveInformation> list = reserveInformationService.selectReserveInformationList(reserveInformation);
        return getDataTable(list);
    }

    /**
     * 导出预约信息列表
     */
    @RequiresPermissions("wx:access:export")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ReserveInformation reserveInformation)
    {
        List<ReserveInformation> list = reserveInformationService.selectReserveInformationList(reserveInformation);
        ExcelUtil<ReserveInformation> util = new ExcelUtil<ReserveInformation>(ReserveInformation.class);
        return util.exportExcel(list, "access");
    }

    /**
     * 新增预约信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存预约信息
     */
    @RequiresPermissions("wx:access:add")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ReserveInformation reserveInformation)
    {
        return toAjax(reserveInformationService.insertReserveInformation(reserveInformation));
    }

    /**
     * 修改预约信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ReserveInformation reserveInformation = reserveInformationService.selectReserveInformationById(id);
        mmap.put("reserveInformation", reserveInformation);
        return prefix + "/edit";
    }

    /**
     * 修改保存预约信息
     */
    @RequiresPermissions("wx:access:edit")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ReserveInformation reserveInformation)
    {
        return toAjax(reserveInformationService.updateReserveInformation(reserveInformation));
    }

    /**
     * 删除预约信息
     */
    @RequiresPermissions("wx:access:remove")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(reserveInformationService.deleteReserveInformationByIds(ids));
    }


    /**
     * 跳转至来访(预约)记录页面
     * @param openId
     * @param mmap
     * @return
     */
    @RequiresPermissions("wx:access:examine")
    @RequestMapping("/examine/{openId}")
    public String returnExamine(@RequestBody @PathVariable("openId") String openId, ModelMap mmap){
        mmap.put("openId", openId);
        return "personnel/userInfo/examine";
    }

    /**
     * 查询来访(预约)记录列表
     * @param openId
     * @return
     */
    @PostMapping("/examineList/{openId}")
    @ResponseBody
    public TableDataInfo examineList(@RequestBody @PathVariable("openId") String openId)
    {
        startPage();
        List<ReserveInformation> list = reserveInformationService.selectExamineByOpenId(openId);
        return getDataTable(list);
    }

}
